﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FillCartridgeBBB.Models;

namespace FillCartridgeBBB.Controllers
{
    public class PrinterController : Controller
    {
       private DataManager _dataManager;

        public PrinterController(DataManager dataManager)
        {
            _dataManager = dataManager;
        }

        public ActionResult List()
        {
            ViewData["Printers"] = _dataManager.Printers.SelectAll();
            return View();
        }

        public ActionResult Delete (int id)
        {
            _dataManager.Printers.Delete(id);
            return RedirectToAction("List");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit (int id)
        {
            ViewData.Model = _dataManager.Printers.SelectById(id);
            ViewData["Departments"] = _dataManager.Departmets.SelectAll();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, Printer printer)
        {
            if (printer.PrinterName == null)
            {
                ModelState.AddModelError("PrinterName", "Невведено імя принтера");
            }

            if (printer.CartridgeName == null)
            {
                ModelState.AddModelError("CartridgeName", "Невведено імя картриджа");
            }

            if (ModelState.IsValid)
            {
                var departmens = Request["DepartmentId"];
                printer.PrinterId = id;
                _dataManager.Printers.Update(printer, departmens);
                return RedirectToAction("List");
            }
            ViewData["Departments"] = _dataManager.Departmets.SelectAll();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            ViewData["Departments"] = _dataManager.Departmets.SelectAll();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create (Printer printer)
        {
            if (printer.PrinterName == null)
            {
                ModelState.AddModelError("PrinterName", "Невведено імя принтера");
            }

            if (printer.CartridgeName == null)
            {
                ModelState.AddModelError("CartridgeName", "Невведено імя картриджа");
            }

            if (ModelState.IsValid)
            {
                var departmens = Request["DepartmentId"];
                _dataManager.Printers.Insert(printer, departmens);
                return RedirectToAction("List");
            }
            ViewData["Departments"] = _dataManager.Departmets.SelectAll();
            return View();

        }

        public ActionResult Details (int id)
        {
            ViewData.Model = _dataManager.Printers.SelectById(id);
            return View(); 
        }

    }
}
